import { config } from "@/config";
import dayjs from "dayjs";
import jwt from "jsonwebtoken";

export class TokenManager {
  static create(user: Models.User): string {
    return jwt.sign({ ...user, time: dayjs().format("YYYYMMDDHHmmss") }, config.jwt.secret);
  }

  static parse(str: string): Models.User {
    try {
      const decode = jwt.verify(str, config.jwt.secret) as Models.User;
      return decode;
    } catch {
      throw new Error("Token解析失败");
    }
  }
}
